package cn.com.bluemoon.daps.sync.core.sync.sql.mysql;

import cn.com.bluemoon.daps.common.datamarking.IDataMarking;
import cn.com.bluemoon.daps.sync.core.sync.ptosc.PropertyValue;
import cn.com.bluemoon.daps.sync.core.sync.sql.SqlStrBuilder;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jarod.Kong
 * @date 2021/6/28 14:18
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MysqlDump implements IDataMarking {

    public final List<PropertyValue> mysqlDumpProps = new ArrayList<PropertyValue>() {{
        add(new PropertyValue("--triggers", null));
        add(new PropertyValue("--no-data", null));
        add(new PropertyValue("--routines", "False"));
        add(new PropertyValue("--events", "False"));
//        add(new PropertyValue("--set-gtid-purged", "OFF"));
        add(new PropertyValue("--add-drop-table", null));
        add(new PropertyValue("--no-create-db", null)); // 2021/6/23 否需要创建数据库，不创建会报错，要手动导入dump文件前创建数据库
        add(new PropertyValue("--quick", null));
        add(new PropertyValue("--add-locks", "False"));
    }};
    private String scriptPath = "/usr/bin/mysqldump";
    private String user, password, database, host;
    private Integer port;
    private List<String> tables;
    private String resultFile;

    /**
     * <pre>
     *     mysqldump  --user=user --password='*****' --host=127.0.0.1 --port=3306 \
     * --databases dbname --tables tabname1 tabname2 \
     * --triggers --no-data --routines=False --events=False \
     * //         * --set-gtid-purged=OFF \
     * --add-drop-table --no-create-db \
     * --quick \
     * --add-locks=False \
     * --result-file=/tmp/db.sql
     * </pre>
     */
    @Override
    public String toString() {
        return toMysqlDumpString(false);
    }

    private String toMysqlDumpString(boolean dataMasking) {
        SqlStrBuilder sb = new SqlStrBuilder();
        sb.append(scriptPath).appendWs().append("--user=").append(user)
                .appendWs().append("--password=");
        if (dataMasking) sb.appendSQ(StrUtil.repeat("*", password.length()));
        else sb.appendSQ(password);
        sb.appendWs().append("--host=").append(host).appendWs().append("--port=").append(port)
                .appendWs().append("--database").appendWS(database);
        if (tables != null && !tables.isEmpty()) {
            sb.append("--tables").appendWS(String.join(" ", tables));
        }
        for (PropertyValue prop : mysqlDumpProps) {
            if (prop.getValue() == null) {
                sb.append(prop.getKey()).appendWs();
            } else {
                sb.append(prop.getKey()).append("=").append(prop.getValue()).appendWs();
            }
        }
        if (resultFile != null) {
            sb.append("--result-file=").append(resultFile).appendWs();
        }
        return sb.toString();
    }

    /**
     * 脱敏数据
     *
     * @return x
     */
    @Override
    public String toString(boolean dataMark) {
        return toMysqlDumpString(dataMark);
    }
}
